import SQLiteStorage from 'react-native-sqlite-storage';
import SQLiteHelper from './SQLiteHelper.js';

export default class CourseMessage {
    constructor() {
        this.message_table_name = 'course_message_tables';
        this.Hepler = new SQLiteHelper();
        this.db = null;
    }

    async createCourseMessageTable() {
        return new Promise((resolve, reject) => {
            if (!this.db) {
                this.Hepler.openDatabase().then(() => { console.log('okCourseInfo'); }, (err) => {
                    console.log(err);
                });
                this.db = this.Hepler.db;
            }
            if (!this.db) {
                reject('open db failed');
            }
            this.db.transaction((tx) => {
                tx.executeSql('CREATE TABLE IF NOT EXISTS ' + this.message_table_name + '(' +
                    'id INTEGER PRIMARY KEY NOT NULL,' +
                    'OnlineClassID INTEGER,' +
                    'SendSubjectID INTEGER,' +
                    'SubjectID INTEGER,' +
                    'class VARCHAR,' +
                    'coursetitle VARCHAR,' +
                    'messageType VARCHAR,' +
                    'moocCourseID VARCHAR,' +
                    'sendtime VARCHAR,' +
                    'teacher VARCHAR,' +
                    'singstatus VARCHAR' +
                    ');'
                    , [], () => {
                        resolve();
                    }, (err) => {
                        reject(err);
                    });
            });

        });
    }

    async insertCourseMessage(OnlineClassID,SendSubjectID,SubjectID,classid,coursetitle,messageType,moocCourseID,sendtime,teacher,singstatus) {
        return new Promise((resolve, reject) => {
            if (!this.db) {
                this.Hepler.openDatabase().then(() => { console.log('ok'); }, (err) => {
                    console.log(err);
                });
                this.db = this.Hepler.db;
            }
            if (!this.db) {
                reject('open db failed');
            }
            if (this.db) {
                this.db.executeSql(
                    'INSERT INTO ' + this.message_table_name + ' (OnlineClassID,SendSubjectID,SubjectID,class,coursetitle,messageType,moocCourseID,sendtime,teacher,singstatus) VALUES(?,?,?,?,?,?,?,?,?,?)',
                    [OnlineClassID,SendSubjectID,SubjectID,classid,coursetitle,messageType,moocCourseID,sendtime,teacher,singstatus],
                    () => {
                        resolve();
                    },
                    (err) => {
                        reject(err);
                    });
            } else {
                reject('db not open');
            }
        });
    }
    // async queryCourseMessage(unitid) {
    //     return new Promise((resolve, reject) => {
    //         if (!this.db) {
    //             this.Hepler.openDatabase().then(() => { console.log('ok'); }, (err) => {
    //                 console.log(err);
    //             });
    //             this.db = this.Hepler.db;
    //         }
    //         if (!this.db) {
    //             reject('open db failed');
    //         }
    //         if (this.db) {
    //             this.db.executeSql('SELECT * FROM ' + this.message_table_name + ' where unitid = ?', [unitid],
    //                 (results) => {
    //                     if (results.rows.length > 0) {
    //                         resolve(results.rows);
    //                     } else {
    //                         resolve(null);
    //                     }
    //                 }, (err) => {
    //                     reject(err);
    //                 });
    //         } else {
    //             reject('db not open');
    //         }
    //     });
    // }
    async queryAllCourseMessage() {
        return new Promise((resolve, reject) => {
            if (!this.db) {
                this.Hepler.openDatabase().then(() => { console.log('ok'); }, (err) => {
                    console.log(err);
                });
                this.db = this.Hepler.db;
            }
            if (!this.db) {
                reject('open db failed');
            }
            if (this.db) {
                this.db.executeSql('SELECT * FROM ' + this.message_table_name+' order by id desc' , [],
                    (results) => {
                        if (results.rows.length > 0) {
                            resolve(results.rows);
                        } else {
                            resolve(null);
                        }
                    }, (err) => {
                        reject(err);
                    });
            } else {
                reject('db not open');
            }
        });
    }

//签到
    async MessageSignIn(signid) {
        return new Promise((resolve, reject) => {
            if (!this.db) {
                this.Hepler.openDatabase().then(() => { console.log('ok'); }, (err) => {
                    console.log(err);
                });
                this.db = this.Hepler.db;
            }
            if (!this.db) {
                reject('open db failed');
            }//DROP TABLE IF EXISTS tbl_name;
            if (this.db) {
                this.db.executeSql('update ' + this.message_table_name+ ' set  singstatus=1 where id=?', [signid],
                    (results) => {
                        if (results) {
                            resolve();
                        } else {
                            reject('sign fail');
                        }
                    }, (err) => {
                        reject(err);
                    });
            } else {
                reject('db not open');
            }
        });
    }

    async deleteCourseMessage() {
        return new Promise((resolve, reject) => {
            if (!this.db) {
                this.Hepler.openDatabase().then(() => { console.log('ok'); }, (err) => {
                    console.log(err);
                });
                this.db = this.Hepler.db;
            }
            if (!this.db) {
                reject('open db failed');
            }//DROP TABLE IF EXISTS tbl_name;
            if (this.db) {
                this.db.executeSql('DROP TABLE IF EXISTS ' + this.message_table_name, [],
                    (results) => {
                        if (results) {
                            resolve();
                        } else {
                            reject('delete fail');
                        }
                    }, (err) => {
                        reject(err);
                    });
            } else {
                reject('db not open');
            }
        });
    }

    async closeCourseMessage() {
        return new Promise((resolve, reject) => {
            if (this.db) {
                console.log('close db ok');
                this.Hepler.closeDatabase();
                resolve();
            }
            if (this.db) {
                reject('close db failed');
            }
        });
    }

}